<?php


namespace app\console\service\machiningScheme;

use app\dao\goods\MachiningSchemeDao;
use app\model\MachiningScheme;


class MachiningSchemeService
{
    /**
     * @var MachiningScheme
     */
    private MachiningScheme $machiningScheme;

    public function __construct(MachiningScheme $machiningScheme)
    {
        $this->machiningScheme = $machiningScheme ;
    }

    public function create($data)
    {
        $machiningSchemeDao = new MachiningSchemeDao();
        return $machiningSchemeDao->save([
            'name'      => $data['name'],
            'ratio'  => $data['ratio'],
        ]);
    }

    public function update($data)
    {
        $machiningSchemeDao = new MachiningSchemeDao();

        if (!empty($data['name']))
        {
            $this->machiningScheme->name = $data['name'];
        }
        if (!empty($data['ratio']))
        {
            $this->machiningScheme->ratio = $data['ratio'];
        }

        $machiningSchemeDao->update($this->machiningScheme->toArray(), $this->machiningScheme->id);

        return $machiningSchemeDao->getById($this->machiningScheme->id);
    }


    public function delete()
    {
        $machiningSchemeDao = new MachiningSchemeDao();
        $machiningSchemeDao->deleteById($this->machiningScheme->id);
    }



}